package log

import (
	"adam2/internal/model"
	"anubis-framework/pkg/io"
	"gorm.io/gorm"
)

// 优化--日志
type OptimizeLog struct {
	db *gorm.DB
}

// 初始化
func (o *OptimizeLog) Init(db *gorm.DB) {
	o.db = db
}

// 向表optimize_account_log表中添加记录
func (o *OptimizeLog) Exec(transactionDate string) {
	io.Infoln("向表optimize_account_log表中添加记录")

	// 返回量化账户
	var optimizeAccountArray model.OptimizeAccountArray
	o.db.Table("optimize_account").Select("*").Order("id_ asc").Find(&optimizeAccountArray)

	for _, optimizeAccount := range optimizeAccountArray {
		o.db.Exec("insert into optimize_account_log(DATE_, ACCOUNT_NAME, HOLD_STOCK_NUMBER, STOCK_ASSETS, "+
			"CAPITAL_ASSETS, TOTAL_ASSETS, TOTAL_STAMP_DUTY, TOTAL_REGISTRATE_FEE_WHEN_BUY, TOTAL_COMMISSION_WHEN_BUY, "+
			"TOTAL_REGISTRATE_FEE_WHEN_SELL, TOTAL_COMMISSION_WHEN_SELL) "+
			"select to_date(?, 'yyyy-mm-dd'), t.ACCOUNT_NAME, t.HOLD_STOCK_NUMBER, t.STOCK_ASSETS, t.CAPITAL_ASSETS, t.TOTAL_ASSETS, t.TOTAL_STAMP_DUTY, "+
			"t.TOTAL_REGISTRATE_FEE_WHEN_BUY, t.TOTAL_COMMISSION_WHEN_BUY, t.TOTAL_REGISTRATE_FEE_WHEN_SELL, t.TOTAL_COMMISSION_WHEN_SELL "+
			"from optimize_account t where t.account_name = ?",
			transactionDate, optimizeAccount.AccountName)
	}
}
